#include "xmlprotocolparser.h"
#include <skyroad/cross_platform/charfunction.hpp>

const CHAR* WXmlParser_LoadCommand( TiXmlElement& element,const CHAR* szCommand,int nLength )
{
    if( NULL == szCommand || nLength == 0 )
        return NULL;
    const CHAR *szResult = element.Parse( szCommand,NULL,TIXML_ENCODING_UTF8 );
    if( NULL == szResult )
        return NULL;
    if( strcmp( element.Value(),"cmd") != 0 )
        return NULL;
    return szResult;
}

const CHAR* WXmlParser_GetCommand( TiXmlElement *pElement,WORD& wCommand )
{
    if( NULL == pElement )
        return NULL;
    int nValue;
    const CHAR* szValue = pElement->Attribute( "id",&nValue );
    if( NULL == szValue )
        return NULL;
    wCommand = nValue;
    return szValue;
}

bool WXmlParser_SetCommand( TiXmlElement *pElement,WORD wCommand )
{
    if( NULL == pElement )
        return false;
    pElement->SetAttribute( "id",wCommand );
    return true;
}

const CHAR* WXmlParser_GetVersion( TiXmlElement *pElement )
{
    if( NULL == pElement )
        return NULL;
    return pElement->Attribute( "ver" );
}

bool WXmlParser_SetVersion( TiXmlElement *pElement,const CHAR* szVersion )
{
    if( NULL == pElement || NULL == szVersion )
        return false;
    pElement->SetAttribute( "ver",szVersion );
    return true;
}

const CHAR* WXmlParser_GetSeqnum( TiXmlElement *pElement,int& nSeqnum )
{
    if( NULL == pElement )
        return NULL;
    int nValue;
    const CHAR* szValue = pElement->Attribute( "seq",&nValue );
    if( NULL == szValue )
        return NULL;
    nSeqnum = nValue;
    return szValue;
}

bool WXmlParser_SetSeqnum( TiXmlElement *pElement,int nSeqnum )
{
    if( NULL == pElement )
        return false;
    pElement->SetAttribute( "seq",nSeqnum );
    return true;
}

const CHAR* WXmlParser_GetFieldValue( TiXmlElement* pElement,const CHAR* szValueName,CHAR* szValue,DWORD dwSize )
{
    if( NULL == pElement || NULL == szValueName )
        return NULL;
    TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
    if( NULL == pValueElement )
        return NULL;
    const CHAR* szVal = pValueElement->Attribute( "val" );
    if( NULL == szVal )
        return NULL;
    if( szValue ){
        if( dwSize <= strlen( szVal ))
            return NULL;
        strcpy( szValue,szVal );
    }
    return szVal;
}

bool WXmlParser_SetFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const CHAR* szValue )
{
    if( NULL == pElement || NULL == szValueName || NULL == szValue )
        return false;
    TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
    if( NULL == pValueElement )
        return NULL;
    pValueElement->SetAttribute( "val",szValue );
    return true;
}

bool WXmlParser_AddFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const CHAR* szValue )
{
    if( NULL == pElement || NULL == szValueName || NULL == szValue )
        return false;
    TiXmlElement element("");
    TiXmlNode *pNode = pElement->InsertEndChild( element );
    if( NULL == pNode )
        return false;
    pNode->ToElement()->SetValue( szValueName );
    pNode->ToElement()->SetAttribute( "val",szValue );
    return true;
}

const CHAR* WXmlParser_GetFieldValue( TiXmlElement* pElement,const CHAR* szValueName,WCHAR* wszValue,DWORD dwSize )
{
    if( NULL == pElement || NULL == szValueName || NULL == wszValue || dwSize == 0 )
        return NULL;
    TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
    if( NULL == pValueElement )
        return NULL;
    const CHAR* szValue = pValueElement->Attribute( "val" );
    if( NULL == szValue )
        return NULL;
    if( ConvertUtf8ToUnicode( szValue,wszValue,dwSize ) <= 0 )
        return NULL;
    return szValue;
}

bool WXmlParser_SetFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const WCHAR* wszValue )
{
    if( NULL == pElement || NULL == szValueName || NULL == wszValue )
        return false;
    TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
    if( NULL == pValueElement )
        return false;
    int nSize = wcslen(wszValue)*3+2;
    CHAR *szValue = new CHAR[ nSize ];
    if( NULL == szValue )
        return false;
    if( ConvertUnicodeToUtf8( wszValue,szValue,nSize) <= 0 ){
        delete[] szValue;
        return false;
    }
    pValueElement->SetAttribute( "val",szValue );
    delete[] szValue;
    return true;
}

bool WXmlParser_AddFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const WCHAR* wszValue )
{
    if( NULL == pElement || NULL == szValueName || NULL == wszValue )
        return false;
    TiXmlElement element("");
    TiXmlNode *pNode = pElement->InsertEndChild( element );
    if( NULL == pNode )
        return false;
    pNode->ToElement()->SetValue( szValueName );

    int nSize = wcslen(wszValue)*3+2;
    CHAR *szValue = new CHAR[ nSize ];
    if( NULL == szValue )
        return false;
    if( ConvertUnicodeToUtf8( wszValue,szValue,nSize) <= 0 ){
        delete[] szValue;
        return false;
    }
    pNode->ToElement()->SetAttribute( "val",szValue );
    delete[] szValue;
    return true;
}

const CHAR* WXmlParser_GetFieldValue( TiXmlElement* pElement,const CHAR* szValueName,int& nValue )
{
    if( NULL == pElement || NULL == szValueName )
        return NULL;
    TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
    if( NULL == pValueElement )
        return NULL;
    return pValueElement->Attribute( "val",&nValue );
}

bool WXmlParser_SetFieldValue( TiXmlElement *pElement,const CHAR* szValueName,int nValue )
{
    if( NULL == pElement || NULL == szValueName )
        return false;
    TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
    if( NULL == pValueElement )
        return false;
    pValueElement->SetAttribute( "val",nValue );
    return true;
}

bool WXmlParser_AddFieldValue( TiXmlElement *pElement,const CHAR* szValueName,int nValue )
{
    if( NULL == pElement || NULL == szValueName )
        return false;
    TiXmlElement element("");
    TiXmlNode *pNode = pElement->InsertEndChild( element );
    if( NULL == pNode )
        return false;
    pNode->ToElement()->SetValue( szValueName );
    pNode->ToElement()->SetAttribute( "val",nValue );
    return true;
}

const CHAR* WXmlParser_GetFieldValue( TiXmlElement* pElement,const CHAR* szValueName,GUID& guid )
{
    if( NULL == pElement || NULL == szValueName )
        return NULL;
    TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
    if( NULL == pValueElement )
        return NULL;
    const CHAR* szValue = pValueElement->Attribute( "val" );
    if( NULL == szValue )
        return NULL;
    WCHAR  wszGuid[64];
    if( ConvertUtf8ToUnicode( szValue,wszGuid,64) <= 0 )
        return NULL;
    CLSIDFromString( wszGuid,&guid );
    return szValue;
}

bool WXmlParser_SetFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const GUID& guid )
{
    if( NULL == pElement || NULL == szValueName )
        return false;
    TiXmlElement *pValueElement = pElement->FirstChildElement( szValueName );
    if( NULL == pValueElement )
        return false;
    WCHAR  wszGuid[64];
    if( StringFromGUID2( guid,wszGuid,64 ) <= 0 )
        return false;
    CHAR szValue[64];
    if( ConvertUnicodeToUtf8( wszGuid,szValue,64 ) <= 0 ){
        return false;
    }
    pValueElement->SetAttribute( "val",szValue );
    return true;
}

bool WXmlParser_AddFieldValue( TiXmlElement *pElement,const CHAR* szValueName,const GUID& guid )
{
    if( NULL == pElement || NULL == szValueName )
        return false;
    TiXmlElement element("");
    TiXmlNode *pNode = pElement->InsertEndChild( element );
    if( NULL == pNode )
        return false;
    pNode->ToElement()->SetValue( szValueName );
    WCHAR  wszGuid[64];
    if( StringFromGUID2( guid,wszGuid,64 ) <= 0 )
        return false;
    CHAR szValue[64];
    if( ConvertUnicodeToUtf8( wszGuid,szValue,64 ) <= 0 ){
        return false;
    }
    pNode->ToElement()->SetAttribute( "val",szValue );
    return true;
}